package Controllers;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import Bean.BeanBook;
import Bean.BeanUser;
import Impl.BookServiceImpl;
@Controller
public class BookModifyController extends HttpServlet {
	private static final long serialVersionUID = 1L;
    BookServiceImpl bookService;
	 @RequestMapping(value = "/BookModify", method = RequestMethod.POST)
		@ResponseBody
		public JSONObject userRegister(HttpServletRequest request,@RequestBody JSONObject in) throws Exception {
		 	ApplicationContext applicationContext=(ApplicationContext)request.getSession().getServletContext().getAttribute("ctx");
		 	bookService=(BookServiceImpl)applicationContext.getBean("BookServiceImpl");
			JSONObject obj = new JSONObject();
			BeanBook book = new BeanBook();
			book.setBook_id(in.getString("bookid"));
			book.setTitle(in.getString("title"));
			book.setAuthor(in.getString("author"));
			book.setPublisher(in.getString("publisher"));
			SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
			book.setPublishing_time(Integer.parseInt(in.getString("publishingtime")));
			book.setBook_price(Float.parseFloat(in.getString("bookprice")));
			book.setCategory_id(Integer.parseInt(in.getString("categoryid")));
			book.setInventory(Integer.parseInt(in.getString("inventory")));
			book.setDescription(in.getString("description"));
			
			bookService.updateBook(book);	
			obj.put("status", "OK");
			return obj;
		}
}
